package com.bw.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bw.bean.CityOrder;
import com.bw.bean.OrderNum;
import com.bw.bean.OrderZb;
import com.bw.bean.Org;
import com.bw.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class OrderController {

    @Autowired
    OrderService service;

    @RequestMapping("/list1")
    public JSONObject list1(){
        OrderNum list = service.list1();

        JSONObject object = new JSONObject();

        object.put("status",0);
        object.put("msg","");
        JSONArray a1 = new JSONArray();

        JSONObject j1 = new JSONObject();
        j1.put("name","接单总数");
        j1.put("value",list.getJd());
        a1.add(j1);

        JSONObject j2 = new JSONObject();
        j2.put("name","发单总数");
        j2.put("value",list.getFd());
        a1.add(j2);

        JSONObject j3 = new JSONObject();
        j3.put("name","完成运单数");
        j3.put("value",list.getWd());
        a1.add(j3);

        object.put("data",a1);

        return object;
    }

    @RequestMapping("/list2")
    public JSONObject list2(){
        List<OrderZb> list = service.list2();

        JSONObject object = new JSONObject();
        object.put("status",0);
        object.put("msg","");
        JSONArray a1 = new JSONArray();

        for (OrderZb zb : list) {
            JSONObject j1 = new JSONObject();
            j1.put("name",zb.getName());
            j1.put("value",zb.getZb());
            a1.add(j1);
        }

        object.put("data",a1);
        return object;
    }

    @RequestMapping("/list3")
    public JSONObject list3(){
        List<CityOrder> list = service.list3();

        JSONObject object = new JSONObject();
        object.put("status",0);
        object.put("msg","");

        JSONObject j1 = new JSONObject();

        JSONArray a1 = new JSONArray();
        for (CityOrder order : list) {
            a1.add(order.getName());
        }
        j1.put("categories",a1);

        JSONArray a2 = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name","城市");
        JSONArray a21 = new JSONArray();
        for (CityOrder order : list) {
            a21.add(order.getTotal());
        }
        jsonObject.put("data",a21);
        a2.add(jsonObject);
        j1.put("series",a2);

        object.put("data",j1);

        return object;
    }

    @RequestMapping("/list4")
    public JSONObject list4(){
        List<Org> list = service.list4();

        JSONObject object = new JSONObject();
        object.put("status",0);
        object.put("msg","");

        JSONObject j1 = new JSONObject();

        JSONArray columns = new JSONArray();

        JSONObject o1 = new JSONObject();
        o1.put("name","机构");
        o1.put("id","orgName");
        columns.add(o1);

        JSONObject o2 = new JSONObject();
        o2.put("name","完运里程");
        o2.put("id","total");
        columns.add(o2);

        j1.put("columns",columns);

        JSONArray rows = new JSONArray();

        for (Org org : list) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("orgName",org.getOrgName());
            jsonObject.put("total",org.getTotal());
            rows.add(jsonObject);
        }

        j1.put("rows",rows);

        object.put("data",j1);

        return object;
    }

}
